"""

@author: lingw
@email: gw.lin@hzgosun.com
@file: noticeManageTest.py.py
@time: 2019-5-24 下午 4:19

这一行开始写关于本文件的说明与解释

"""

import os
import unittest
from time import sleep

from selenium.webdriver.common.keys import Keys

from common.functions import save_screenshot
from common.decorators import mkdir_classname, log4gs
from common.settings import TAG, DATA_DIR
from common.vue.select import DropDownSelect, OperateSelect
from pages.noticeManagePage import NoticeManagePage

@log4gs
@mkdir_classname(TAG)
class NoticeManageTest(unittest.TestCase):
    """公告管理页面测试"""

    @classmethod
    def setUpClass(cls):
        cls.log.info('=' * 30 + "开始公告管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page = NoticeManagePage('/VxManage/NoticeManage')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束公告管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info('>>>>>开始执行用例：{} {}'.format(self.id(), self.shortDescription()))

    def tearDown(self):
        self.log.info('<<<<<结束执行用例：{} {}'.format(self.id(), self.shortDescription()))


    @unittest.skip
    def test_1_turn_page(self):
        """翻页功能
        http://181.181.0.216:8786/tc/detail/74-4541/
        """
        self.log.debug("当前共有：" + self.page.turnPage.total_page +"页数据" )
        self.log.debug("当前处于第：" + self.page.turnPage.current_page + "页数据")
        self.assertEqual(self.page.turnPage.current_page, '1')
        sleep(1)
        try:
            self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '3')

            sleep(1)
            self.page.turnPage.prev_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.input.clear()
            self.page.turnPage.input.send_keys(6)
            self.page.turnPage.input.send_keys(Keys.ENTER)
            self.assertEqual(self.page.turnPage.current_page, '6')
        except Exception:
            self.log.error("当前模块最大为1页，无法进行翻页测试", exc_info=True)
            save_screenshot('翻页功能测试失败')

    @unittest.skip
    def test_2_add_notice(self):
        """增加公告测试
        http://181.181.0.216:8786/tc/detail/74-4522/
        """

        self.page.table.add_notice_btn.click()
        self.page.addNotice.add_notice_name.send_keys("测试增加公告")
        self.page.addNotice.add_notice_content.send_keys("一大串测试公告的内容啊")
        self.page.addNotice.add_notice_grid.click()
        self.page.addNotice.add_notice_grid_select.select("江西省-广西南宁市网格-广西南宁青秀区网格-广西南宁青秀中山街道")
        sleep(1)
        self.page.addNotice.add_notice_name.click()
        sleep(2)

        DropDownSelect(self.page.addNotice.add_notice_face_to, "专干小程序")
        sleep(2)

        self.page.addNotice.add_notice_pic.send_keys(os.path.join(DATA_DIR, "测试公告图片.jpg"))
        sleep(1)

        self.page.addNotice.confirm.click()
        sleep(1)

        self.page.searchTools.notice_grid.click()
        self.page.searchTools.notice_grid_select.select("江西省-广西南宁市网格-广西南宁青秀区网格-广西南宁青秀中山街道")
        self.page.searchTools.notice_name.send_keys("测试增加公告")
        self.page.searchTools.notice_search.click()
        sleep(1)

        add_result = self.page.table.get_row("测试增加公告")

        try:
            self.assertTrue(add_result, msg="无法找到新添加的公告，添加失败")
            self.log.info("添加公告用例执行成功")
        except Exception:
            self.log.error("添加公告用例执行失败", exc_info=True)
            save_screenshot('添加公告用例执行失败')
        self.page.searchTools.notice_grid.clear()
        self.page.searchTools.notice_name.clear()

    @unittest.skip
    def test_3_search_notice(self):
        """查询公告
        http://181.181.0.216:8786/tc/detail/74-4540/
        """

        self.page.searchTools.notice_grid.click()
        self.page.searchTools.notice_grid_select.select("广西南宁青秀中山街道")
        self.page.searchTools.notice_name.send_keys("测试增加公告")
        self.page.searchTools.notice_search.click()
        sleep(2)

        search_result = self.page.table.get_row("测试增加公告")

        try:
            self.assertTrue(search_result, msg="无法找到公告，查询失败")
            self.log.info("查询公告用例执行成功")
        except Exception:
            self.log.error("查询公告用例执行失败", exc_info=True)
            save_screenshot('查询banner图用例执行失败')
        self.page.searchTools.notice_grid.clear()
        self.page.searchTools.notice_name.clear()
        sleep(1)

    @unittest.skip
    def test_4_modify_notice(self):
        """修改公告
        http://181.181.0.216:8786/tc/detail/74-4532/
        """

        self.page.searchTools.notice_grid.click()
        self.page.searchTools.notice_grid_select.select('广西南宁青秀中山街道')
        self.page.searchTools.notice_name.send_keys("测试增加公告")
        self.page.searchTools.notice_search.click()
        sleep(2)

        result = self.page.table.get_row("测试增加公告")
        OperateSelect(result, "查看编辑")
        sleep(2)

        self.page.modifyNotice.add_notice_name.clear()
        self.page.modifyNotice.add_notice_name.send_keys("测试修改公告")
        # self.page.searchTools.notice_grid.clear()
        #注释这行代码的原因：修改页面的网格不属于ivu-input-icon-normal，所以无法使用ET.GRID
        # self.page.modifyNotice.add_notice_grid.click()
        # self.page.modifyNotice.add_notice_grid_select.select("江西省")
        sleep(1)
        self.page.modifyNotice.add_notice_name.click()
        sleep(2)
        self.page.modifyNotice.confirm.click()
        sleep(1)

        # self.page.searchTools.add_notice_grid.clear()
        # self.page.searchTools.add_notice_grid.click()
        # self.page.searchTools.add_notice_grid.select("江西省-广西南宁市网格-广西南宁青秀区网格-广西南宁青秀中山街道")
        self.page.searchTools.notice_name.clear()
        self.page.searchTools.notice_name.send_keys("测试修改公告")
        self.page.searchTools.notice_search.click()
        sleep(2)

        modify_result = self.page.table.get_row("测试修改公告")

        try:
            self.assertTrue(modify_result, msg="无法找到修改后的公告")
            self.log.info("修改公告用例执行成功")
        except Exception:
            self.log.error("修改公告用例执行失败", exc_info=True)
            save_screenshot('修改banner图用例执行失败')
        self.page.searchTools.notice_grid.clear()
        self.page.searchTools.notice_name.clear()


    # @unittest.skip
    def test_5_blockup_start_notice(self):
        """停用/启用公告
        http://181.181.0.216:8786/tc/detail/74-4531/
        """

        self.page.searchTools.notice_grid.click()
        # self.page.searchTools.notice_grid_select.select("江西省-广西南宁市网格-广西南宁青秀区网格-广西南宁青秀中山街道")
        self.page.searchTools.notice_grid_select.select('广西南宁青秀中山街道')
        self.page.searchTools.notice_name.send_keys("测试修改公告")
        self.page.searchTools.notice_search.click()
        sleep(2)

        search_result = self.page.table.get_row("测试修改公告")
        OperateSelect(search_result, "停用")
        sleep(2)

        blockup_result = self.page.table.get_row("停用")

        try:
            self.assertTrue(blockup_result, msg="停用失败，该公告依旧处于启用状态")
            self.log.info("停用公告用例执行成功")
        except Exception:
            self.log.error("停用公告用例执行失败", exc_info=True)
            save_screenshot('停用公告用例执行失败')
        sleep(1)

        OperateSelect(blockup_result, "启用")
        sleep(2)

        startup_result = self.page.table.get_row("启用")

        try:
            self.assertTrue(startup_result, msg="启用失败，该公告依旧处于停用状态")
            self.log.info("启用公告用例执行成功")
        except Exception:
            self.log.error("启用公告用例执行失败", exc_info=True)
            save_screenshot('启用公告用例执行失败')


if __name__ == '__main__':
    unittest.main()
